package com.harmony.suppertiger;


import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Picture;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.RectFloat;

/**
 * Created by Yaoyuanling on 2021/12/25.
 */

public class TiledLayer {
    //region 字段
    private int mX;
    private int mY;
    private int mWidth;
    private int mHeight;
    private PixelMapHolder mBitmap;
    private RectFloat mDest;
    private RectFloat mSrc;
    private int mRows;
    private int mCols;
    private int[][] mTiledCell;
    private int[] mTiledX;
    private int[] mTiledY;
    private Paint paint;
    //endregion

    //region Getter and Setter

    public int getRows() {
        return mRows;
    }

    public void setRows(int rows) {
        this.mRows = rows;
    }

    public int getCols() {
        return mCols;
    }

    public void setCols(int cols) {
        this.mCols = cols;
    }

    public int getTiledCell(int cols,int row) {
        return mTiledCell[row][cols];
    }

    public void setTiledCell(int[][] tiledCell) {
        this.mTiledCell = tiledCell;
    }

    public PixelMapHolder getBitmap() {
        return mBitmap;
    }

    public void setBitmap(PixelMapHolder bitmap) {
        mBitmap = bitmap;
    }

    public int getX() {
        return mX;
    }

    public void setX(int x) {
        mX = x;
    }

    public int getY() {
        return mY;
    }

    public void setY(int y) {
        mY = y;
    }

    public int getWidth() {
        return mWidth;
    }

    public void setWidth(int width) {
        mWidth = width;
    }

    public int getHeight() {
        return mHeight;
    }

    public void setHeight(int height) {
        mHeight = height;
    }



    //endregion
    public TiledLayer(PixelMapHolder bitmap, int cols , int rows , int width, int height) {
        super();
        this.mBitmap = bitmap;
        this.mHeight = height;
        this.mWidth = width;
        this.mRows = rows;
        this.mCols = cols;
        this.mTiledCell = new int[rows][cols];
        int w = bitmap.getPixelMap().getImageInfo().size.width / width;
        int h = bitmap.getPixelMap().getImageInfo().size.height / height;
        mTiledX = new int[w*h+1];
        mTiledY = new int[w*h+1];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                mTiledX[i * w + j + 1] = j * width;
                mTiledY[i * w + j + 1] = i * height;
            }
        }
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(60);
        paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setTextSize(20);//设置字号
    }

    public void move(float x, float y) {
        mY += y;
        mX += x;
        outOfBounds();
    }

    public void draw(Canvas canvas) {
        for (int i = 0; i < mRows; i++) {
            for (int j = 0; j < mCols; j++) {
                int tiledIndex = mTiledCell[i][j];
                if (tiledIndex == 0) {
                    continue;
                }
                int x = mTiledX[tiledIndex];
                int y = mTiledY[tiledIndex];
                int ix = mX + j * mWidth;
                int iy = mY + i * mHeight;
                mSrc = new RectFloat(x, y, x + mWidth, y + mHeight);
                mDest = new RectFloat(ix,iy , ix+mWidth, iy + mHeight);
                Picture picture = new Picture();
                System.out.println("+++++++++++++++TiledLayer+++++++++++++++++++" + mBitmap.getPixelMap().getImageInfo());
                canvas.drawPixelMapHolderRect(mBitmap,mSrc,mDest,paint);//闪退
//                canvas.drawPixelMapHolder(mBitmap, 740, 360, paint);//闪退
//                canvas.drawPixelMapHolderRoundRectShape(mBitmap,mSrc,mDest,0,0);//闪退
//                canvas.drawPicture();
            }
        }
    }

    /**
     * 地图的边界处理
     */
    private void outOfBounds() {
        if(getX()>0){
            setX(0);
        }else if(getX()<800-getCols()*getWidth()){
            setX(800-getCols()*getWidth());
        }
        if(getY()>0){
            setY(0);
        }else if(getY()<480-getRows()*getWidth()){
            setY(480-getRows()*getWidth());
        }
    }

}
